package myjava.h_exception;

/**
 * @Auther: cty
 * @Date: 2019/11/8 下午9:19
 * @Description: 自定义异常
 * @version: 1.0
 */
public class D_Custom {
    public static void main(String[] args) {
        Person p = new Person();
        p.setAge(-10);
    }
}

//定义一个测试类
class Person {
    private int age;

    public int getAge(){
        return age;
    }

    public void setAge(int age){
        if(age<0){
            throw new IllegalAgeExecption("年龄不能为负数");  //throw是手动抛出异常
        }
    }
}

//定义一个非法年龄异常
class IllegalAgeExecption extends RuntimeException {  //RuntimeException是继承运行时异常；Exception是继承编译时异常，必须try-catch或throws处理
    public IllegalAgeExecption(){
    }

    public IllegalAgeExecption(String msg){
        super(msg);
    }
}
